package com.uikoo9.plugin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.uikoo9.manage.y.util.NumberTool;
import com.uikoo9.manage.y.util.StringUtil;
import com.uikoo9.util.core.data.QStringUtil;
import com.uikoo9.util.plugin.tree.QTreeCheck;

/**
 * tree
 * @author qiaowenbin
 */
public class QTree1 {
    private int id;
    private String url;
    private String text;
    private boolean checked = false;
    private List<QTree1> children;

    /**
     *初始化数据
     * id,pid,text
     */
    private List data;
    /**
     *初始化数据
     * select id,pid,txt where pid=?
     */
    private String sql;

    public QTree1() {
        super();
    }
    public QTree1(int id, String url, String text,List data,  QTreeCheck check) {
        super();
        this.id = id;
        this.url = url;
        this.text = text;
        this.data = data;
        if(check != null) this.checked = check.isCheck(id);
        this.children = findChildrenByTableAndCheck(id, check);
    }

    public QTree1(int id, String url, String text,  QTreeCheck check ,String sql) {
        super();
        this.id = id;
        this.url = url;
        this.text = text;
        this.sql = sql;
        if(check != null) this.checked = check.isCheck(id);
        this.children = findChildrenByTableAndCheck(id, check);
    }



    private List<QTree1> findChildrenByTableAndCheck(int id, QTreeCheck check){
        List<QTree1> list = new ArrayList<QTree1>();
        if(data!=null && !data.isEmpty()){
            //todo 解析列表
            for(Object a :getData()){
                Map m = null;
                if(a instanceof Map){
                    m = (Map)a;
                }
                if(m!=null && m.get("pid").equals(id+"")){
                    list.add(new QTree1(NumberTool.safeToInteger(m.get("id"), 0), StringUtil.valueOf(m.get("url"))
                            , StringUtil.valueOf(m.get("text")),getData(), check));
                }
            }
        }else if(!StringUtil.isEmpty(getSql())) {
            List<Record> menus = Db.find(getSql(), id);
            for(Record menu : menus){
                list.add(new QTree1(menu.getInt("id"),StringUtil.valueOf( menu.get("url")),StringUtil.valueOf(  menu.getStr("text")), check,getSql()));
            }
        }

        return list;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }
    public String getText() {
        return text;
    }
    public void setText(String text) {
        this.text = text;
    }
    public boolean isChecked() {
        return checked;
    }
    public void setChecked(boolean checked) {
        this.checked = checked;
    }
    public List<QTree1> getChildren() {
        return children;
    }
    public void setChildren(List<QTree1> children) {
        this.children = children;
    }

    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public String getSql() {
        return sql;
    }

    public void setSql(String sql) {
        this.sql = sql;
    }
}
